/**
 * @description 刮刮乐组件
 * @date 2024.9.10
 * @author wz
 * @midify 2025.5.15
 * @editor wz
 */
import { _decorator, Component, Node, NodeEventType, EventTouch, Vec2, UITransform, v3, Graphics, CCInteger, v2, EventHandler } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CScrape')
export class CScrape extends Component {

    @property({ type: Node, tooltip: '刮奖遮罩' })
    nodeMask: Node;

    @property({ type: Node, tooltip: '刮奖区域' })
    nodeArea: Node;

    @property({ type: CCInteger, tooltip: '刮点(位置left-bottom)向右上方扩散的宽度' })
    scatchWidth: number = 5;

    @property({ type: CCInteger, tooltip: '刮点(位置left-bottom)向右上方扩散的高度' })
    scatchHeight: number = 5;

    @property({ type: CCInteger, step: 1, range: [0, 100], slide: true, tooltip: '刮开区域能获奖的百分比' })
    thresholdValue: number = 80

    @property([EventHandler])
    scrapeEvents: EventHandler[] = [];

    private _points: boolean[][] = [];
    private _row: number = 0;
    private _col: number = 0;
    private _isScraped: boolean = false;
    start() {

        this.node.on(NodeEventType.TOUCH_START, this.fillScrape.bind(this), this);
        this.node.on(NodeEventType.TOUCH_MOVE, this.fillScrape.bind(this), this);
        this.node.on(NodeEventType.TOUCH_END, this.fillScrape.bind(this), this);
        this.node.on(NodeEventType.TOUCH_CANCEL, this.fillScrape.bind(this), this);

        this.initScrape();
    }

    initScrape(scatchWidth?: number, scatchHeight?: number) {
        this._isScraped = false;
        this.nodeMask.getComponent(Graphics).clear();
        if (scatchWidth)
            this.scatchWidth = Math.floor(scatchWidth);
        if (scatchHeight)
            this.scatchHeight = Math.floor(scatchHeight);
        this.calMaskPoints();
    }

    private calMaskPoints() {
        let area = this.nodeArea.getComponent(UITransform);
        this._col = Math.floor(area.width / this.scatchWidth);
        this._row = Math.floor(area.height / this.scatchHeight);
        this._points.length = 0;
        for (let i = 0; i < this._row; i++) {
            this._points[i] = [];
            for (let j = 0; j < this._col; j++) {
                this._points[i][j] = false;
            }
        }
    }

    private checkPoints(pos: Vec2) {
        let area = this.nodeArea.getComponent(UITransform);
        let unitWith = area.width / this._col;
        let unitHeight = area.height / this._row;
        let index = 0;
        for (let i = 0; i < this._row; i++) {
            for (let j = 0; j < this._col; j++) {
                if (!this._points[i][j]) {
                    let leftX = -area.width / 2 + j * unitWith - unitWith / 2;
                    let rightX = -area.width / 2 + j * unitWith + unitWith / 2;
                    let topY = area.height / 2 - (i + 1) * unitHeight + unitHeight / 2;
                    let botY = area.height / 2 - (i + 1) * unitHeight - unitHeight / 2;
                    if (pos.x >= leftX && pos.x <= rightX && pos.y >= botY && pos.y <= topY) {
                        this._points[i][j] = true;
                    }
                }
                if (this._points[i][j])
                    index++;
            }
        }
        if (index * 100 / (this._row * this._col) >= this.thresholdValue) {
            if (!this._isScraped) {
                this._isScraped = true;
                this.scrapeEvents.forEach((a) => {
                    a.emit([this.node, a.customEventData])
                })
            }
        }
    }

    private fillScrape(event: EventTouch) {
        let pos: Vec2 = event.getUILocation();
        let localPos = this.nodeMask.getComponent(UITransform).convertToNodeSpaceAR(v3(pos.x, pos.y))
        // console.log(`localPos:${localPos}`);
        this.nodeMask.getComponent(Graphics).fillRect(localPos.x, localPos.y, this.scatchWidth, this.scatchHeight)
        this.checkPoints(v2(localPos.x, localPos.y));
    }



}


